<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Question extends Model
{

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];

    public function examStatus()
    {
        return $this->hasOne('App\Models\QuestionExam', 'id');
    }

    public function checkStatus()
    {
        return $this->hasOne('App\Models\QuestionCheck', 'id');
    }

    public function buildDescription(array $map)
    {
        $result = '';
        // 题目第一行
        $result .= $this->id;
        $result .= ' ';
        $result .= $map[$this->question_type];
        $result .= ' ';
        $result .= $this->question_form - 1;
        $result .= ' ';
        $result .= \preg_replace('/\.\w+$/', '', $this->graph ?: '0.jpg');
        $result .= ' ';
        $pass = $this->examStatus->pass_num;
        $check = $this->examStatus->check_num;
        $result .= $check > 0 ? $pass / $check : 0;
        $result .= "\n";
        // 题目第二行
        $result .= $this->ask;
        if ($this->question_form === 1) {
            foreach (\json_decode($this->options) as $opt) {
                $result .= ' ' . $opt;
            }
        }
        $result .= "\n";
        // 题目第三行
        $result .= $this->answer;
        $result .= "\n";
        return $result;
    }
}
